package com.cqs.leetcode.dp;

/**
 * @author lixiaowen
 * @create 2019-12-13
 */
public class HouseRobber198 {


    public int robDp(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        if (nums.length == 1) return nums[0];
        int[] res = new int[nums.length];
        res[0] = nums[0];
        res[1] = Math.max(nums[0], nums[1]);
        for (int i = 2; i < nums.length; i++) {
            res[i] = Math.max(nums[i] + res[i - 2], res[i - 1]);
        }
        return res[nums.length - 1];
    }

    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int[] res = new int[nums.length];
        for (int i = 0; i < nums.length; i++) res[i] = -1;
        return rob(nums, nums.length, res);
    }

    /**
     * 递推式
     * F(n) = max(F(n-2) + room(n), F(n-1))
     * F(n) 标识n间房间可以偷到的最大金额
     * room(n) 标识第n间房间的金额
     *
     * @param nums
     * @param rooms 偷数组前rooms个房间
     * @param res   保存中间结果
     * @return
     */
    private int rob(int[] nums, int rooms, int[] res) {
        if (rooms == 0) return 0;
        //使用中间结果
        if (res[rooms - 1] != -1) return res[rooms - 1];
        if (rooms == 1) return res[0] = nums[0];
        return res[rooms - 1] = Math.max(nums[rooms - 1] + rob(nums, rooms - 2, res), rob(nums, rooms - 1, res));
    }
}
